Page({
  data: {
    show: false,
    title: '',
    checked: true, // 复选框默认选中
    orderData: {
      orderNum: '',
      buyerName: '',
      amount: '',
      detail: '',
      orderInfo: null, // 完整订单信息
      storeName: '',
      username: ''
    }
  },

  onLoad(options) {
    // 从页面参数中获取数据
    console.log('接收到的参数:', options);
    
    if (options.orderNum) {
      let orderInfo = null;
      if (options.orderInfo) {
        try {
          orderInfo = JSON.parse(decodeURIComponent(options.orderInfo));
        } catch (e) {
          console.error('解析orderInfo失败:', e);
        }
      }
      
      this.setData({
        'orderData.orderNum': options.orderNum,
        'orderData.buyerName': decodeURIComponent(options.buyerName || ''),
        'orderData.amount': options.amount || '',
        'orderData.detail': decodeURIComponent(options.detail || ''),
        'orderData.orderInfo': orderInfo,
        'orderData.storeName': decodeURIComponent(options.storeName || ''),
        'orderData.username': decodeURIComponent(options.username || '')
      })
    }
    
    // 延迟显示弹出层，实现动画效果
    setTimeout(() => {
      this.setData({ show: true })
    }, 50)
  },

  // 点击关闭按钮
  onClose() {
    this.setData({ show: false })
    setTimeout(() => {
      wx.navigateBack()
    }, 300)
  },
  
  // 阻止冒泡
  stopPropagation() {},

  // 切换复选框状态
  toggleCheckbox() {
    this.setData({
      checked: !this.data.checked
    })
    console.log('复选框状态:', this.data.checked)
  },

  // 全部订单分享
  onShareAll() {
    const { orderInfo, storeName, username, buyerName } = this.data.orderData;
    
    console.log('点击全部订单分享', {
      orderData: this.data.orderData,
      orderInfo: orderInfo,
      maijia_id: orderInfo?.maijia_id,
      getPhone: this.data.checked
    })
    
    // 检查必要参数
    if (!orderInfo || !orderInfo.store_id) {
      wx.showToast({
        title: '店铺信息不完整',
        icon: 'none'
      })
      return;
    }
    
    // 检查买家ID
    if (!orderInfo.maijia_id) {
      wx.showToast({
        title: '买家信息不完整',
        icon: 'none'
      })
      console.error('买家ID为空:', orderInfo);
      return;
    }
    
    // 跳转到对账页面，传递店铺信息和买家ID（用于过滤）
    const maijiaId = orderInfo.maijia_id;
    console.log('跳转参数:', {
      storeId: orderInfo.store_id,
      storeName: storeName,
      username: username,
      maijia_id: maijiaId
    });
    
    const targetPath = `/pages/reconciliation/reconciliation?storeId=${orderInfo.store_id}&storeName=${encodeURIComponent(storeName)}&username=${encodeURIComponent(username)}&maijia_id=${maijiaId}`;
    // 使用 navigateToMiniProgram 以全屏方式打开（摆脱半屏容器）
    wx.navigateToMiniProgram({
      appId: 'wx26980c7935e571cc',
      path: targetPath,
      // envVersion: 'release', // 如需体验版/开发版可改成 trial/develop
      success: () => {},
      fail: (err) => {
        console.warn('navigateToMiniProgram 失败，回退到 redirectTo:', err);
        // 回退方案：仍在半屏内跳转，至少保证功能可达
        wx.redirectTo({ url: targetPath });
      }
    })
  },

  // 本条订单分享
  onShareCurrent() {
    const { orderInfo, storeName, username } = this.data.orderData;
    
    console.log('点击本条订单分享', {
      orderData: this.data.orderData,
      getPhone: this.data.checked
    })
    
    if (!orderInfo) {
      wx.showToast({
        title: '订单数据不完整',
        icon: 'none'
      })
      return;
    }
    
    // 跳转到订单详情页面，传递完整数据（使用全屏打开）
    const detailPath = `/pages/orderDetail/orderDetail?orderInfo=${encodeURIComponent(JSON.stringify(orderInfo))}&storeName=${encodeURIComponent(storeName)}&username=${encodeURIComponent(username)}`;
    wx.navigateToMiniProgram({
      appId: 'wx26980c7935e571cc',
      path: detailPath,
      // envVersion: 'release',
      success: () => {},
      fail: (err) => {
        console.warn('navigateToMiniProgram 失败，回退到 redirectTo:', err);
        wx.redirectTo({ url: detailPath });
      }
    })
  }
})
